package com.sgb.packers.view.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.ScreenUtils;
import com.sgb.packers.R;
import com.sgb.packers.databinding.PackersFragmentPointWorkEditBinding;
import com.sgb.packers.entity.SalaryStandardEntity;
import com.sgb.packers.entity.WorkRecordsEntity;
import com.sgb.packers.view.activity.PersonGetWorkActivity;
import com.sgb.packers.view.activity.PointWorkEditActivity;
import com.sgb.packers.view.adapter.ImageAdapter;
import com.sgb.packers.viewmodel.PointWorkEditFragViewModel;
import com.sgb.packers.widget.InputWorkDayPopWindow;
import com.sgb.packers.widget.SalaryPopWindow;
import com.sgb.packers.widget.TimeSalaryPopWindow;
import com.sgb.packers.widget.WorkTakePopWindow;
import com.sgb.younghow.calendarview.Calendar;
import com.swgk.core.common.base.TeamBaseFragment;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.interfaces.DialogClick;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.NewDialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author : younghow
 * @date : 2021/11/18 11:20
 * description:
 */
public class PointWorkEditFragment extends TeamBaseFragment<PointWorkEditFragViewModel, PackersFragmentPointWorkEditBinding> implements ItemInputTextView.OnWholeItemClickListener {

    private List<Calendar> mSelectCalendar;
    private SalaryPopWindow salaryPopWindow;
    private WorkTakePopWindow workTakePopWindow;
    private TimeSalaryPopWindow timeSalaryPopWindow;
    private InputWorkDayPopWindow inputWorkDayPopWindow;
    private TimeSalaryPopWindow inputAddWorkDayPopWindow;
    private ImageAdapter mPicAdapter;
    private List<File> fileList;
    private List<String> mUploadImgList;
    private String haveWorkOvertime = "1";
    private String no;
    private String picture;
    private String projectName;
    private String projectNo;
    private double workNumber = 1.0;
    private double workOvertimeDuration;
    private double workOvertimeSalary;
    private double workOvertimeStandard;
    private String workOvertimeType;
    private double workStandard;
    private double workStandardSalary;
    private String workType = "1";
    private WorkRecordsEntity workRecordsEntity;
    private String workTime;
    private PersonGetWorkActivity personGetWorkActivity;
    private PointWorkEditActivity pointWorkEditActivity;
    private boolean isCompleteSalary;
    private SalaryStandardEntity mSalaryStandardEntity;
    private boolean isSalaryChange;
    private boolean isSetDuration;

    public static PointWorkEditFragment newInstance(String projectName, int year, int month, int day, String projectNo) {
        PointWorkEditFragment pointWorkEditFragment = new PointWorkEditFragment();
        Bundle bundle = new Bundle();
        bundle.putString("projectName", projectName);
        bundle.putInt("year", year);
        bundle.putInt("month", month);
        bundle.putInt("day", day);
        bundle.putString("projectNo", projectNo);
        pointWorkEditFragment.setArguments(bundle);
        return pointWorkEditFragment;
    }

    public static PointWorkEditFragment newInstance(String projectName, int year, int month, int day, String projectNo, String no) {
        PointWorkEditFragment pointWorkEditFragment = new PointWorkEditFragment();
        Bundle bundle = new Bundle();
        bundle.putString("projectName", projectName);
        bundle.putInt("year", year);
        bundle.putInt("month", month);
        bundle.putInt("day", day);
        bundle.putString("projectNo", projectNo);
        bundle.putString("no", no);
        pointWorkEditFragment.setArguments(bundle);
        return pointWorkEditFragment;
    }

    @Override
    protected Class<PointWorkEditFragViewModel> createVM() {
        return PointWorkEditFragViewModel.class;
    }

    @Override
    protected void initView() {

        mBinding.tvOneWork.setSelected(true);
        mBinding.tvAddWorkOne.setSelected(true);

        mUploadImgList = new ArrayList<>();
        fileList = new ArrayList<>();

        GridLayoutManager manager = new GridLayoutManager(getContext(), 4);
        mBinding.rvPhoto.setLayoutManager(manager);
        mPicAdapter = new ImageAdapter(getContext(), new ArrayList<>(), new ArrayList<>(), 5);
        mBinding.rvPhoto.setHasFixedSize(false);
        mBinding.rvPhoto.addItemDecoration(new GridSpacingItemDecoration(3, ScreenUtils.dip2px(getContext(), 2), false));
        mBinding.rvPhoto.setAdapter(mPicAdapter);
        mPicAdapter.setOpenListener(() -> {
            if (personGetWorkActivity != null) {
                personGetWorkActivity.setType(1);
            }
        });

        mBinding.etRemarks.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mBinding.tvRemarksWordsNumber.setText(String.format(Locale.CHINA, "%d/%d", mBinding.etRemarks.getText().toString().length(), 300));
            }
        });
    }

    private void initLiveData() {
        mViewModel.uploadUrls.observe(getViewLifecycleOwner(), strings -> {
            StringBuilder stringBuilder = new StringBuilder();
            for (String string : strings) {
                stringBuilder.append(",").append(string);
            }
            stringBuilder.delete(0, 1);

            for (String netUrl : mPicAdapter.getNetUrls()) {
                stringBuilder.append(",").append(netUrl);
            }

            picture = stringBuilder.toString();

            upload();
        });

        mViewModel.uploadSuccess.observe(getViewLifecycleOwner(), aBoolean -> getActivity().finish());

        if (TextUtils.isEmpty(no)) {
            personGetWorkActivity = (PersonGetWorkActivity) getActivity();
            if (personGetWorkActivity != null) {
                personGetWorkActivity.mIntentLiveData.observe(getViewLifecycleOwner(), intent -> {
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(intent);
                    fileList.clear();
                    mUploadImgList.clear();
                    for (LocalMedia localMedia : selectList) {
                        File file = new File(localMedia.getRealPath());
                        fileList.add(file);
                        mUploadImgList.add(localMedia.getRealPath());
                    }
                    mPicAdapter.setDatas(selectList, mUploadImgList);
                    mPicAdapter.setFileList(fileList);
                });
            }
        }else {
            pointWorkEditActivity = (PointWorkEditActivity) getActivity();
            if (pointWorkEditActivity != null) {
                pointWorkEditActivity.mIntentLiveData.observe(getViewLifecycleOwner(), intent -> {
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(intent);
                    fileList.clear();
                    mUploadImgList.clear();
                    for (LocalMedia localMedia : selectList) {
                        File file = new File(localMedia.getRealPath());
                        fileList.add(file);
                        mUploadImgList.add(localMedia.getRealPath());
                    }
                    mPicAdapter.setDatas(selectList, mUploadImgList);
                    mPicAdapter.setFileList(fileList);
                });
            }
        }

        mViewModel.pointDetail.observe(getViewLifecycleOwner(), workRecordsEntity -> {
            PointWorkEditFragment.this.workRecordsEntity = workRecordsEntity;
            double addSalary = 0;
            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workRecordsEntity.getSalary()));
            if (workRecordsEntity.getWorkStandardSalary() != 0) {
                workStandardSalary = workRecordsEntity.getWorkStandardSalary();
            }

            if ("1".equals(workRecordsEntity.getWorkOvertimeType())) {
                addSalary = workRecordsEntity.getWorkStandardSalary() / workRecordsEntity.getWorkOvertimeStandard();
                workOvertimeStandard = addSalary;
            } else {
                workOvertimeSalary = workRecordsEntity.getWorkOvertimeSalary();
                addSalary = workRecordsEntity.getWorkOvertimeSalary();
            }

            if (workStandardSalary != 0 && addSalary != 0) {
                mBinding.tvSalaryStandard.setText("已选择");
                mBinding.tvWorkHourNumber.setVisibility(View.VISIBLE);
                mBinding.ivDeleteSalary.setVisibility(View.VISIBLE);
                isCompleteSalary = true;
                mBinding.tvWorkHourNumber.setText(String.format(Locale.CHINA, "上班%.2f元/个工   加班%.2f元/小时", workStandardSalary, addSalary));
            } else {
                mBinding.tvSalaryStandard.setText("");
                mBinding.tvSalaryStandard.clearComposingText();
                mBinding.tvSalaryStandard.setHint("请选择工资标准");
                mBinding.tvWorkHourNumber.setVisibility(View.GONE);
                mBinding.ivDeleteSalary.setVisibility(View.GONE);
            }

            workTime = workRecordsEntity.getWorkDate();
            workType = workRecordsEntity.getWorkType();
            haveWorkOvertime = workRecordsEntity.getHaveWorkOvertime();
            no = workRecordsEntity.getNo();
            picture = workRecordsEntity.getPicture();
            projectName = workRecordsEntity.getProjectName();
            projectNo = workRecordsEntity.getProjectNo();
            workOvertimeDuration = workRecordsEntity.getWorkOvertimeDuration();
            workNumber = workRecordsEntity.getWorkDuration();
            workOvertimeSalary = workRecordsEntity.getWorkOvertimeSalary();
            workOvertimeStandard = workRecordsEntity.getWorkOvertimeStandard();
            workOvertimeType = workRecordsEntity.getWorkOvertimeType();
            workStandard = workRecordsEntity.getWorkStandard();
            workStandardSalary = workRecordsEntity.getWorkStandardSalary();

            mBinding.etRemarks.setText(workRecordsEntity.getRemark());

            if ("1".equals(workType)){
                mBinding.tvOneWork.setSelected(true);
                mBinding.tvInputWorkDay.setSelected(false);
                mBinding.tvSelectHour.setSelected(false);
                mBinding.tvRest.setSelected(false);
            }else if ("2".equals(workType)){
                mBinding.tvOneWork.setSelected(false);
                mBinding.tvInputWorkDay.setSelected(true);
                mBinding.tvSelectHour.setSelected(false);
                mBinding.tvRest.setSelected(false);
                mBinding.tvInputWorkDay.setText(String.format(Locale.CHINA,"%.1f个工",workNumber));
            }else if ("4".equals(workType)){
                mBinding.tvOneWork.setSelected(false);
                mBinding.tvInputWorkDay.setSelected(false);
                mBinding.tvSelectHour.setSelected(true);
                mBinding.tvRest.setSelected(false);
                mBinding.tvSelectHour.setText(String.format(Locale.CHINA,"%.1f小时",workNumber));
            }else if ("3".equals(workType)){
                mBinding.tvOneWork.setSelected(false);
                mBinding.tvInputWorkDay.setSelected(false);
                mBinding.tvSelectHour.setSelected(false);
                mBinding.tvRest.setSelected(true);
            }

            if ("1".equals(haveWorkOvertime)){
                mBinding.tvAddWorkInputDay.setSelected(false);
                mBinding.tvAddWorkOne.setSelected(true);
            }else if ("2".equals(haveWorkOvertime)){
                mBinding.tvAddWorkOne.setSelected(false);
                mBinding.tvAddWorkInputDay.setSelected(true);
                mBinding.tvAddWorkInputDay.setText(String.format(Locale.CHINA,"%.1f小时",workOvertimeDuration));
            }

            isSetDuration = true;
            if (!TextUtils.isEmpty(workRecordsEntity.getPicture())) {
                String[] pictures = workRecordsEntity.getPicture().split(",");
                List<String> list = new ArrayList<>(Arrays.asList(pictures));
                mPicAdapter.setNetImgList(list);
            }
        });

        mViewModel.delSuccess.observe(getViewLifecycleOwner(), aBoolean -> getActivity().finish());

        mViewModel.salaryStandard.observe(getViewLifecycleOwner(), salaryStandardEntity -> {
            if (salaryStandardEntity != null) {
                mSalaryStandardEntity = salaryStandardEntity;
                isCompleteSalary = true;
                mBinding.ivDeleteSalary.setVisibility(View.VISIBLE);
                mBinding.tvWorkHourNumber.setVisibility(View.VISIBLE);
                mBinding.tvSalaryStandard.setText("已选择");

                workOvertimeSalary = salaryStandardEntity.getWorkOvertimeSalary();
                workOvertimeStandard = salaryStandardEntity.getWorkOvertimeStandard();
                workOvertimeType = salaryStandardEntity.getWorkOvertimeType();
                workStandard = salaryStandardEntity.getWorkStandard();
                workStandardSalary = salaryStandardEntity.getWorkStandardSalary();

                mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary));
                if ("2".equals(workOvertimeType)) {
                    mBinding.tvWorkHourNumber.setText(String.format(Locale.CHINA, "上班%.2f元/个工   加班%.2f元/小时", salaryStandardEntity.getWorkStandardSalary(), salaryStandardEntity.getWorkOvertimeSalary()));
                } else {
                    mBinding.tvWorkHourNumber.setText(String.format(Locale.CHINA, "上班%.2f元/个工   加班%.2f元/小时", salaryStandardEntity.getWorkStandardSalary(), salaryStandardEntity.getWorkStandardSalary() / salaryStandardEntity.getWorkOvertimeStandard()));
                }
            } else {
                mBinding.tvSalaryStandard.clearComposingText();
            }
        });
    }

    private void initListener() {
        if (TextUtils.isEmpty(no)) {
            mBinding.iitSelectDate.setOnWholeItemClickListener(this);
        }
        mBinding.tvSalaryStandard.setOnClickListener(this);
        mBinding.tvOneWork.setOnClickListener(this);
        mBinding.tvInputWorkDay.setOnClickListener(this);
        mBinding.tvSelectHour.setOnClickListener(this);
        mBinding.tvRest.setOnClickListener(this);
        mBinding.tvAddWorkOne.setOnClickListener(this);
        mBinding.tvAddWorkInputDay.setOnClickListener(this);
        mBinding.tvUpload.setOnClickListener(this);
        mBinding.tvDelete.setOnClickListener(this);
        mBinding.tvSaveChange.setOnClickListener(this);
        mBinding.ivDeleteSalary.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mSelectCalendar = new ArrayList<>();
        if (getArguments() != null) {
            projectName = getArguments().getString("projectName");
            projectNo = getArguments().getString("projectNo");
            no = getArguments().getString("no");

            if (!TextUtils.isEmpty(no)) {
                mBinding.llChangeDelete.setVisibility(View.VISIBLE);
                mBinding.tvUpload.setVisibility(View.GONE);
            } else {
                mBinding.llChangeDelete.setVisibility(View.GONE);
                mBinding.tvUpload.setVisibility(View.VISIBLE);
            }

            mBinding.tvFragmentTradePublishSelectType.setText(projectName);

            if (TextUtils.isEmpty(no)) {
                mViewModel.salaryStandard(projectNo);
            }

            int year;
            int month;
            int day;
            if (getArguments().getInt("year") != 0) {
                year = getArguments().getInt("year");
                month = getArguments().getInt("month");
                day = getArguments().getInt("day");
                Calendar calendar = new Calendar();
                calendar.setCurrentDay(true);
                calendar.setCurrentMonth(true);
                calendar.setDay(day);
                calendar.setMonth(month);
                calendar.setYear(year);
                mSelectCalendar.add(calendar);
            } else {
                String[] date = DateUtils.timestamp2DateNumber(System.currentTimeMillis());
                Calendar calendar = new Calendar();
                calendar.setCurrentDay(true);
                calendar.setCurrentMonth(true);
                calendar.setDay(Integer.parseInt(date[2]));
                calendar.setMonth(Integer.parseInt(date[1]));
                calendar.setYear(Integer.parseInt(date[0]));
                mSelectCalendar.add(calendar);
                day = Integer.parseInt(date[2]);
                month = Integer.parseInt(date[1]);
                year = Integer.parseInt(date[0]);
            }

            mBinding.iitSelectDate.setContent(String.format(Locale.CHINA, "%s年%s月%s日", year, month, day));
        }

        if (!TextUtils.isEmpty(no)) {
            mViewModel.getPointDetail(no, "1");
            mBinding.iitSelectDate.setArrowVisible(false);
        }

        initLiveData();
        initListener();
    }

    @Override
    protected int layoutId() {
        return R.layout.packers_fragment_point_work_edit;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_salary_standard) {
            if (salaryPopWindow == null) {
                salaryPopWindow = new SalaryPopWindow(getActivity(), condition -> {
                });
                if (workRecordsEntity != null && isCompleteSalary) {
                    salaryPopWindow.setDefault(workRecordsEntity.getWorkOvertimeType(), workRecordsEntity.getWorkStandard(), workRecordsEntity.getWorkStandardSalary(), "2".equals(workRecordsEntity.getWorkOvertimeType()) ? workRecordsEntity.getWorkOvertimeSalary() : workRecordsEntity.getWorkOvertimeStandard());
                }

                if (mSalaryStandardEntity != null && isCompleteSalary) {
                    salaryPopWindow.setDefault(mSalaryStandardEntity.getWorkOvertimeType(), mSalaryStandardEntity.getWorkStandard(), mSalaryStandardEntity.getWorkStandardSalary(), "2".equals(mSalaryStandardEntity.getWorkOvertimeType()) ? mSalaryStandardEntity.getWorkOvertimeSalary() : mSalaryStandardEntity.getWorkOvertimeStandard());
                }

                salaryPopWindow.setSure((type, hour, addWorkSalary) -> {
                    mBinding.ivDeleteSalary.setVisibility(View.VISIBLE);
                    workOvertimeType = type;
                    if (hour != 0.0 || addWorkSalary.length > 0) {
                        mBinding.tvSalaryStandard.setText("已选择");
                    }

                    if (hour != 0.0) {
                        workStandard = hour;
                    }

                    if (addWorkSalary.length > 0) {
                        if (addWorkSalary.length == 1) {
                            workStandardSalary = addWorkSalary[0];
                            mBinding.tvWorkHourNumber.setVisibility(View.VISIBLE);
                            mBinding.tvWorkHourNumber.setText(String.format(Locale.CHINA, "上班%.2f元/个工", addWorkSalary[0]));
                            if ("1".equals(workType)) {
                                mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0]));
                            } else if ("2".equals(workType)) {
                                mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0] * workNumber));
                            } else if ("3".equals(workType)) {
                                mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0] / workOvertimeStandard * workNumber));
                            } else if ("4".equals(workType)) {
                                mBinding.tvMomentSalaryNumber.setText("");
                            }
                        } else if (addWorkSalary.length == 2) {
                            isCompleteSalary = true;
                            isSalaryChange = true;
                            workStandardSalary = addWorkSalary[0];
                            double addSalary;
                            if ("1".equals(workOvertimeType)) {
                                workOvertimeStandard = addWorkSalary[1];
                                addSalary = addWorkSalary[0] / workOvertimeStandard;
                            } else {
                                workOvertimeSalary = addWorkSalary[1];
                                addSalary = addWorkSalary[1];
                            }
                            mBinding.tvWorkHourNumber.setVisibility(View.VISIBLE);
                            mBinding.tvWorkHourNumber.setText(String.format(Locale.CHINA, "上班%.2f元/个工   加班%.2f元/小时", addWorkSalary[0], addSalary));

                            if ("1".equals(haveWorkOvertime)) {
                                if ("1".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0]));
                                } else if ("2".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0] * workNumber));
                                } else if ("3".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0] / workOvertimeStandard * workNumber));
                                } else if ("4".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText("");
                                }
                            } else {
                                if ("1".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0] + addSalary * workOvertimeDuration));
                                } else if ("2".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0] * workNumber + addSalary * workOvertimeDuration));
                                } else if ("3".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addWorkSalary[0] / workOvertimeStandard * workNumber + addSalary * workOvertimeDuration));
                                } else if ("4".equals(workType)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", addSalary * workOvertimeDuration));
                                }
                            }
                        } else {
                            mBinding.tvWorkHourNumber.setText("");
                        }

                        salaryPopWindow.dismiss();
                    }
                });
            }

            if (workStandard != 0){
                salaryPopWindow.setDefault(workOvertimeType,workStandard,workStandardSalary,"2".equals(workOvertimeType) ? workOvertimeSalary : workOvertimeStandard);
            }

            salaryPopWindow.show();
        } else if (v.getId() == R.id.tv_one_work) {
            if (!mBinding.tvOneWork.isSelected()) {
                mBinding.tvOneWork.setSelected(true);
                mBinding.tvInputWorkDay.setSelected(false);
                mBinding.tvSelectHour.setSelected(false);
                mBinding.tvRest.setSelected(false);
            }

            workNumber = 1;
            workType = "1";

            if (isCompleteSalary) {
                if ("1".equals(haveWorkOvertime)) {
                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary));
                } else {
                    if (!TextUtils.isEmpty(workOvertimeType)) {
                        if ("2".equals(workOvertimeType)) {
                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary + workOvertimeSalary * workOvertimeDuration));
                        } else if ("1".equals(workOvertimeType)) {
                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                        } else {
                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary));
                        }
                    }
                }
            }
        } else if (v.getId() == R.id.tv_input_work_day) {
            if (inputWorkDayPopWindow == null) {
                inputWorkDayPopWindow = new InputWorkDayPopWindow(getContext(), condition -> {
                });

                if (isSetDuration && workRecordsEntity != null && "2".equals(workRecordsEntity.getWorkType())){
                    inputWorkDayPopWindow.setDefault(workRecordsEntity.getWorkDuration());
                }
                inputWorkDayPopWindow.setDialogClick(new DialogClick<String>() {
                    @Override
                    public void confirmClick(String s, Dialog dialog) {
                        if (TextUtils.isEmpty(s)) {
                            MToast.showToast(getContext(), "请选择上班时长");
                            return;
                        }
                        inputWorkDayPopWindow.dismiss();
                        if ("0".equals(s)) {
                            mBinding.tvOneWork.setSelected(false);
                            mBinding.tvInputWorkDay.setSelected(false);
                            mBinding.tvSelectHour.setSelected(false);
                            mBinding.tvRest.setSelected(true);
                            workNumber = 0;
                            workType = "3";
                            if (isCompleteSalary) {
                                if ("1".equals(haveWorkOvertime)) {
                                    if (!TextUtils.isEmpty(workOvertimeType)) {
                                        if ("2".equals(workOvertimeType)) {
                                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workOvertimeSalary * workOvertimeDuration));
                                        } else if ("1".equals(workOvertimeType)) {
                                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                                        } else {
                                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber));
                                        }
                                    }
                                } else {
                                    mBinding.tvMomentSalaryNumber.setText("");
                                }
                            }
                            return;
                        }

                        if (!TextUtils.isEmpty(s)) {
                            mBinding.tvInputWorkDay.setText(String.format("%s个工", s));
                            if (!mBinding.tvInputWorkDay.isSelected()) {
                                mBinding.tvOneWork.setSelected(false);
                                mBinding.tvInputWorkDay.setSelected(true);
                                mBinding.tvSelectHour.setSelected(false);
                                mBinding.tvRest.setSelected(false);
                            }

                            workNumber = Double.parseDouble(s);
                            workType = "2";

                            if (isCompleteSalary) {
                                if ("1".equals(haveWorkOvertime)) {
                                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber));
                                } else {
                                    if (!TextUtils.isEmpty(workOvertimeType)) {
                                        if ("2".equals(workOvertimeType)) {
                                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber + workOvertimeSalary * workOvertimeDuration));
                                        } else if ("1".equals(workOvertimeType)) {
                                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                                        } else {
                                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber));
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }

            inputWorkDayPopWindow.show();
        } else if (v.getId() == R.id.tv_select_hour) {
            if (timeSalaryPopWindow == null) {
                timeSalaryPopWindow = new TimeSalaryPopWindow(getContext());
                if (isSetDuration && workRecordsEntity != null && "4".equals(workRecordsEntity.getWorkType())){
                    timeSalaryPopWindow.setDefault(workNumber);
                }
                timeSalaryPopWindow.setDialogClick(new DialogClick<String>() {
                    @Override
                    public void confirmClick(String s, Dialog dialog) {
                        if (TextUtils.isEmpty(s)) {
                            MToast.showToast(getContext(), "请选择上班时长");
                            return;
                        }
                        mBinding.tvSelectHour.setText(s);
                        timeSalaryPopWindow.dismiss();
                        if (!mBinding.tvSelectHour.isSelected()) {
                            mBinding.tvOneWork.setSelected(false);
                            mBinding.tvInputWorkDay.setSelected(false);
                            mBinding.tvSelectHour.setSelected(true);
                            mBinding.tvRest.setSelected(false);
                        }

                        workNumber = Double.parseDouble(s.replace("小时", ""));
                        workType = "4";
                        if (isCompleteSalary) {
                            if ("1".equals(haveWorkOvertime)) {
                                mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber));
                            } else {
                                if (!TextUtils.isEmpty(workOvertimeType)) {
                                    if ("2".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workOvertimeSalary * workOvertimeDuration));
                                    } else if ("1".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                                    } else {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber));
                                    }
                                }
                            }
                        }
                    }
                });
            }

            timeSalaryPopWindow.showPopwindow(mBinding.etRemarks, Gravity.BOTTOM, 0, 0);
        } else if (v.getId() == R.id.tv_rest) {
            if (!mBinding.tvRest.isSelected()) {
                mBinding.tvOneWork.setSelected(false);
                mBinding.tvInputWorkDay.setSelected(false);
                mBinding.tvSelectHour.setSelected(false);
                mBinding.tvRest.setSelected(true);
            }

            workNumber = 0;
            workType = "3";
            if (isCompleteSalary) {
                if ("2".equals(haveWorkOvertime)) {
                    if (!TextUtils.isEmpty(workOvertimeType)) {
                        if ("2".equals(workOvertimeType)) {
                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workOvertimeSalary * workOvertimeDuration));
                        } else if ("1".equals(workOvertimeType)) {
                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                        } else {
                            mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber));
                        }
                    }
                } else {
                    mBinding.tvMomentSalaryNumber.setText("");
                }
            }

        } else if (v.getId() == R.id.tv_add_work_input_day) {
            if (inputAddWorkDayPopWindow == null) {
                inputAddWorkDayPopWindow = new TimeSalaryPopWindow(getContext());
                if (isSetDuration && workRecordsEntity != null && "2".equals(workRecordsEntity.getHaveWorkOvertime())){
                    inputAddWorkDayPopWindow.setDefault(workOvertimeDuration);
                }
                inputAddWorkDayPopWindow.setTitle("选择加班时长");
                inputAddWorkDayPopWindow.setDialogClick(new DialogClick<String>() {
                    @Override
                    public void confirmClick(String s, Dialog dialog) {
                        if (TextUtils.isEmpty(s)) {
                            MToast.showToast(getContext(), "请选择加班时长");
                            return;
                        }

                        mBinding.tvAddWorkInputDay.setText(s);
                        inputAddWorkDayPopWindow.dismiss();
                        if (!mBinding.tvAddWorkInputDay.isSelected()) {
                            mBinding.tvAddWorkOne.setSelected(false);
                            mBinding.tvAddWorkInputDay.setSelected(true);
                        }

                        haveWorkOvertime = "2";
                        workOvertimeDuration = Double.parseDouble(s.replace("小时", ""));

                        if (isCompleteSalary) {
                            if (!TextUtils.isEmpty(workOvertimeType)) {
                                if ("1".equals(workType)) {
                                    if ("2".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary + workOvertimeSalary * workOvertimeDuration));
                                    } else if ("1".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                                    } else {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary));
                                    }
                                } else if ("2".equals(workType)) {
                                    if ("2".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber + workOvertimeSalary * workOvertimeDuration));
                                    } else if ("1".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                                    } else {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber));
                                    }
                                } else if ("4".equals(workType)) {
                                    if ("2".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workOvertimeSalary * workOvertimeDuration));
                                    } else if ("1".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber + workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                                    } else {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workStandard * workNumber));
                                    }
                                } else if ("3".equals(workType)) {
                                    if ("2".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workOvertimeSalary * workOvertimeDuration));
                                    } else if ("1".equals(workOvertimeType)) {
                                        mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workOvertimeStandard * workOvertimeDuration));
                                    } else {
                                        mBinding.tvMomentSalaryNumber.setText("");
                                    }
                                }
                            }
                        }
                    }
                });
            }
            inputAddWorkDayPopWindow.showPopwindow(mBinding.etRemarks, Gravity.BOTTOM, 0, 0);
        } else if (v.getId() == R.id.tv_add_work_one) {
            if (!mBinding.tvAddWorkOne.isSelected()) {
                mBinding.tvAddWorkInputDay.setSelected(false);
                mBinding.tvAddWorkOne.setSelected(true);
            }
            haveWorkOvertime = "1";
            workOvertimeDuration = 0;
            if (isCompleteSalary) {
                if ("1".equals(workType)) {
                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary));
                } else if ("2".equals(workType)) {
                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary * workNumber));
                } else if ("4".equals(workType)) {
                    mBinding.tvMomentSalaryNumber.setText(String.format(Locale.CHINA, "%.2f元", workStandardSalary / workOvertimeStandard * workNumber));
                } else if ("3".equals(workType)) {
                    mBinding.tvMomentSalaryNumber.setText("");
                }
            }
        } else if (v.getId() == R.id.tv_upload) {
            List<File> file = mPicAdapter.getFileList();
            if (file.size() > 0) {
                mViewModel.uploadFile(file);
            } else {
                upload();
            }
        } else if (v.getId() == R.id.tv_delete) {
            RoundCornerDialog cornerDialog = new NewDialogHelper<String>(getContext(), R.layout.library_dialog_salary_sure_no_title)
                    .text(R.id.tv_message, "确认删除该记工")
                    .text(R.id.tv_logout_cancel, "取消")
                    .text(R.id.tv_logout_confirm, "确定")
                    .onClick(R.id.tv_logout_confirm, new DialogClick<String>() {
                        @Override
                        public void confirmClick(String s, Dialog dialog) {
                            mViewModel.delPoint(no);
                            dialog.dismiss();
                        }
                    })
                    .onClick(R.id.tv_logout_cancel, new DialogClick<String>() {
                        @Override
                        public void confirmClick(String s, Dialog dialog) {
                            dialog.dismiss();
                        }
                    })
                    .build();
            cornerDialog.show();
        } else if (v.getId() == R.id.tv_save_change) {
            List<File> file = mPicAdapter.getFileList();
            if (file.size() > 0) {
                mViewModel.uploadFile(file);
            } else {
                upload();
            }
        } else if (v.getId() == R.id.iv_delete_salary) {
            if (salaryPopWindow != null) {
                salaryPopWindow.clear();
            }
            mBinding.tvSalaryStandard.setText("");
            mBinding.tvSalaryStandard.clearComposingText();
            mBinding.tvSalaryStandard.setHint("请选择工资标准");
            mBinding.tvWorkHourNumber.setVisibility(View.GONE);
            mBinding.tvWorkHourNumber.setText("");
            mBinding.tvMomentSalaryNumber.setText("");
            mBinding.ivDeleteSalary.setVisibility(View.GONE);
            workOvertimeSalary = 0;
            workOvertimeStandard = 0;
            workOvertimeType = "";
            workStandard = 0;
            workStandardSalary = 0;
            isCompleteSalary = false;
            isSalaryChange = false;
            if (mSalaryStandardEntity != null) {
                mViewModel.delSalaryStandard(mSalaryStandardEntity.getNo());
            }
        }

    }

    @Override
    public void OnWholeItemClick(View view) {
        if (view.getId() == R.id.iit_select_date) {
            if (workTakePopWindow == null) {
                workTakePopWindow = new WorkTakePopWindow(getContext());
                Calendar[] selectCalendar = new Calendar[mSelectCalendar.size()];
                for (int i = 0; i < mSelectCalendar.size(); i++) {
                    selectCalendar[i] = mSelectCalendar.get(i);
                }
                workTakePopWindow.setDefaultCalendar(selectCalendar);
                workTakePopWindow.setSelectYearMonth();
                workTakePopWindow.setSelect(selectCalendar);
                workTakePopWindow.setTransCalender(list -> {
                    mSelectCalendar = list;
                    if (list != null && list.size() > 0) {
                        if (list.size() == 1) {
                            mBinding.iitSelectDate.setContent(String.format(Locale.CHINA, "%d年%d月%d日", list.get(0).getYear(), list.get(0).getMonth(), list.get(0).getDay()));
                        } else {
                            mBinding.iitSelectDate.setContent(String.format(Locale.CHINA, "选择%d天", list.size()));
                        }
                        workTakePopWindow.dismiss();
                    } else {
                        MToast.showToast(getContext(), "请选择日期");
                    }
                });
            }

            workTakePopWindow.showPopwindow(mBinding.iitSelectDate, Gravity.BOTTOM, 0, 0);
        }
    }

    private void upload() {
        StringBuilder stringBuilder = new StringBuilder();
        for (Calendar calendar : mSelectCalendar) {
            stringBuilder.append(",").append(calendar.getYear()).append("-").append(calendar.getMonth()).append("-").append(calendar.getDay());
        }

        stringBuilder.delete(0, 1);
        workTime = stringBuilder.toString();

        Map<String, Object> map = new HashMap<>();
        map.put("haveWorkOvertime", haveWorkOvertime);
        if (!TextUtils.isEmpty(no)) {
            map.put("no", no);
        }
        map.put("picture", picture);
        map.put("projectName", projectName);
        map.put("projectNo", projectNo);
        map.put("recordType", 1);
        map.put("remark", mBinding.etRemarks.getText().toString());
        if (!TextUtils.isEmpty(mBinding.tvMomentSalaryNumber.getText().toString())) {
            map.put("salary", Double.valueOf(mBinding.tvMomentSalaryNumber.getText().toString().replace("元", "")));
        }

        map.put("userNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, ""));
        map.put("workDuration", workNumber);
        map.put("workOvertimeDuration", workOvertimeDuration);
        if (isCompleteSalary) {
            map.put("workOvertimeSalary", workOvertimeSalary);
            map.put("workOvertimeStandard", workOvertimeStandard);
            map.put("workOvertimeType", workOvertimeType);
            map.put("workStandard", workStandard);
            map.put("workStandardSalary", workStandardSalary);
        }
        map.put("workTime", workTime);
        map.put("workType", workType);
        mViewModel.uploadWork(map);


        if (isSalaryChange && TextUtils.isEmpty(no)) {
            Map<String, Object> salaryParma = new HashMap<>();
            if (mSalaryStandardEntity != null) {
                salaryParma.put("no", mSalaryStandardEntity.getNo());
            }

            salaryParma.put("projectName", projectName);
            salaryParma.put("projectNo", projectNo);
            salaryParma.put("type", "1");
            salaryParma.put("workOvertimeSalary", workOvertimeSalary);
            salaryParma.put("workOvertimeStandard", workOvertimeStandard);
            salaryParma.put("workOvertimeType", workOvertimeType);
            salaryParma.put("workStandard", workStandard);
            salaryParma.put("workStandardSalary", workStandardSalary);
            mViewModel.addSalaryStandard(salaryParma);
        }
    }
}
